# -*- coding: utf-8 -*-

import sys
import pygame
from bullet import Bullet
from alien import Alien
from time import  sleep

#监视键盘和鼠标事件
def check_events(ai_settings,screen,status,sb,play_button,ship,aliens,bullets):
	for event in pygame.event.get():
		if event.type == pygame.K_q:
			sys.exit()

		elif event.type == pygame.KEYDOWN:
			check_keydown_events(event,ai_settings,screen,ship,bullets)

		elif event.type == pygame.KEYUP:
			check_keyup_events(event,ship)

		elif event.type == pygame.MOUSEBUTTONDOWN:
			mouse_x, mouse_y = pygame.mouse.get_pos()
			check_play_button(ai_settings, screen, status, sb,play_button, ship, aliens, bullets, mouse_x, mouse_y)

#键盘弹起
def check_keyup_events(event,ship):
	if event.key == pygame.K_RIGHT:
		ship.moving_right = False
	elif event.key == pygame.K_LEFT:
		ship.moving_left = False

#键盘按下
def check_keydown_events(event,ai_settings,screen,ship,bullets):
	if event.key == pygame.K_RIGHT:
		ship.moving_right = True
	elif event.key == pygame.K_LEFT:
		ship.moving_left = True

	elif event.key == pygame.K_SPACE and len(bullets) < ai_settings.bullet_allowed:
		new_bullet = Bullet(ai_settings,screen,ship)
		bullets.add(new_bullet)

#更新屏幕上的图像,并切换到新屏幕
def update_screen(ai_settings,screen,status,sb,ship,alien,bullets,play_button):
	#
	screen.fill(ai_settings.bg_color)

	for bullet in bullets.sprites():
		bullet.draw_bullet()

	ship.blitme()
	alien.draw(screen)
	sb.show_score()

	if not status.game_active:
		play_button.draw_button()

def check_play_button(ai_settings, screen, stats,sb,play_button, ship, aliens, bullets, mouse_x, mouse_y):
	button_click = play_button.rect.collidepoint(mouse_x,mouse_y)

	if button_click and not stats.game_active:
		pygame.mouse.set_visible(False)

		stats.reset_stats()
		stats.game_active = True

		sb.pre_score()
		sb.pre_hight_score()
		sb.pre_level()
		sb.pre_ships()

		aliens.empty()
		bullets.empty()

		create_fleet(ai_settings, screen, ship, aliens)
		ship.center_ship()




def update_bullets(ai_settings,screen,stats,sb,ship,aliens,bullets):
	for bullet in bullets.copy():
		if bullet.rect.bottom <= 0:
			bullets.remove(bullet)
	check_bullet_alien_collisions(ai_settings,screen,stats,sb,ship,aliens,bullets)

#重新开始
def check_bullet_alien_collisions(ai_settings,screen,stats,sb,ship,aliens,bullets):
	collesions = pygame.sprite.groupcollide(bullets,aliens,True,True)
	if collesions :
		for aliens in collesions.values():
			stats.score += int(ai_settings.alien_points*len(aliens))
			sb.pre_score()

	if len(aliens)==0:
		bullets.empty()
		ai_settings.increase_speed()

		stats.level += 1
		sb.pre_level()

		create_fleet(ai_settings,screen,ship,aliens)

#创建外星人数组
def create_fleet(ai_settings,screen,ship,aliens):
	alien = Alien(ai_settings,screen)
	number_aliens_x = get_number_aliens_x(ai_settings,alien.rect.width)
	number_rows = get_number_rows(ai_settings,ship.rect.height,alien.rect.height)

	for row_number in range(number_rows):
		for alien_number in range(number_aliens_x):
			create_alien(ai_settings,screen,aliens,alien_number,row_number)

#获取外星人X坐标
def  get_number_aliens_x(ai_settings,alien_width):
	available_space_x = ai_settings.screen_width - 2 * alien_width
	number_aliens_x = int(available_space_x / (2 * alien_width))
	return number_aliens_x

#创建一个外星人
def create_alien(ai_settings,screen,aliens,alien_number,row_number):
	alien = Alien(ai_settings, screen)
	alien_width = alien.rect.width
	alien.x = alien_width + 2 * alien_width * alien_number
	alien.rect.x = alien.x
	alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
	aliens.add(alien)

def get_number_rows(ai_settings,ship_height,alien_height):
	available_space_y = (ai_settings.screen_height - (3 * alien_height) - ship_height)
	number_rows = int(available_space_y / (2 * alien_height))
	return number_rows

def update_aliens(ai_settings,screen,status,sb,ship,aliens,bullets):
	check_fleet_edges(ai_settings,aliens)
	aliens.update()
	if pygame.sprite.spritecollideany(ship,aliens):
		ship_hit(ai_settings,screen,status,sb,ship,aliens,bullets)
	check_aliens_bottom(ai_settings,screen,status,sb,ship,aliens,bullets)

#是否到底部
def check_aliens_bottom(ai_settings,screen,status,sb,ship,aliens,bullets):
	screen_rect = screen.get_rect()
	for alien in aliens.sprites():
		if alien.rect.bottom >= screen_rect.bottom:
			ship_hit(ai_settings,screen,status,sb,ship,aliens,bullets)
			break

#是否换行
def check_fleet_edges(ai_settings,aliens):
	for alien in aliens.sprites():
		if alien.check_edges():
			change_fleet_direction(ai_settings,aliens)
			break

#更换方向
def change_fleet_direction(ai_settings,aliens):
	for alien in aliens.sprites():
		alien.rect.y += ai_settings.fleet_drop_speed

	ai_settings.fleet_direction *= -1

#是否还有命
def ship_hit(ai_setting,screen,status,sb,ship,aliens,bullets):
	if status.ships_left >0:
		status.ships_left -= 1
		sb.pre_ships()

	else:
		sb.update_hight_score()
		status.game_active = False
		pygame.mouse.set_visible(True)

	aliens.empty()
	bullets.empty()

	create_fleet(ai_setting, screen, ship, aliens)
	ship.center_ship()

	sleep(1)



#是否失败
def update_alients(ai_setting,status,screen,ship,aliens,bullets):
	if pygame.sprite.spritecollideany(ship,aliens):
		ship_hit(ai_setting,status,screen,ship,aliens,bullets)
